﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

namespace SLLottery.Controls
{
	public partial class NewsControl : UserControl
	{
		private double newsHeight = 0.0;
		private double newsStackTop = 0.0;
		private int posItem = 0;
		private Storyboard sb;
		private DispatcherTimer showNewsListTimer;
		private List<string> NewsStrings;
		private SLLottery.LotterySrv.LotteryServiceClient srv_news;
		public NewsControl()
		{
			InitializeComponent();
			if (!DesignerProperties.GetIsInDesignMode(this))
			{
				sb = new Storyboard();
				sb.Completed += new EventHandler(sb_Completed);
				showNewsListTimer = new DispatcherTimer();
				showNewsListTimer.Interval = TimeSpan.FromSeconds(5);
				showNewsListTimer.Tick += new EventHandler(showNewsListTimer_Tick);
				srv_news = WcfProxy.GetProxy;
				srv_news.GetNewsInfoCompleted += new EventHandler<LotterySrv.GetNewsInfoCompletedEventArgs>(srv_news_GetNewsInfoCompleted);
				this.Loaded += new RoutedEventHandler(NewsControl_Loaded);
				this.RefreshNewsList();
			}
			//NewsStrings = new List<string>() { "0|系统公告，系统测试运行中....\n系统公告，系统测试运行中....\n系统公告，系统测试运行中....",
			//                                   "1|好消息，好消息，系统终于正式上线了。\n好消息，好消息，系统终于正式上线了。\n", 
			//                                   "2|平台最新域名地址,点击我.|http://www.baidu.com" };
		}

		void NewsControl_Loaded(object sender, RoutedEventArgs e)
		{

		}
		private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			if ((this.LayoutRoot.ActualWidth > 0.0) && (this.LayoutRoot.ActualHeight > 0.0))
			{
				RectangleGeometry geometry = new RectangleGeometry();
				geometry.Rect = new Rect(0.0, 0.0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight);
				geometry.RadiusY = 10.0;
				geometry.RadiusX = 10.0;
				LayoutRoot.Clip = geometry;
			}
		}

		private void m_client_NewsChanged(object sender, EventArgs e)
		{
			this.RefreshNewsList();
		}

		private void news_list_MouseEnter(object sender, MouseEventArgs e)
		{
			if (this.newsHeight > this.news_list.ActualHeight)
			{
				this.showNewsListTimer.Stop();
			}
		}

		private void news_list_MouseLeave(object sender, MouseEventArgs e)
		{
			if (this.newsHeight > this.news_list.ActualHeight)
			{
				this.showNewsListTimer.Start();
			}
		}

		private void news_list_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			if ((this.news_list.ActualWidth > 0.0) && (this.news_list.ActualHeight > 0.0))
			{
				RectangleGeometry geometry = new RectangleGeometry();
				geometry.Rect = new Rect(0.0, 0.0, this.news_list.ActualWidth, this.news_list.ActualHeight);
				news_list.Clip = geometry;
			}
		}

		public void RefreshNewsList()
		{
			srv_news.GetNewsInfoAsync();
		}
		void srv_news_GetNewsInfoCompleted(object sender, LotterySrv.GetNewsInfoCompletedEventArgs e)
		{
			NewsStrings = e.Result.ToList();
			this.newsStackTop = 0.0;
			this.posItem = 0;
			if (NewsStrings.Count >= 1)
			{
				NewsItem item;
				this.stackNews.Children.Clear();
				this.stackNews.Width = this.news_list.ActualWidth;
				foreach (string str in NewsStrings)
				{
					item = new NewsItem();
					item.Margin = new Thickness(0.0, 2.0, 0.0, 0.0);
					item.SetNewsString(str);
					this.stackNews.Children.Add(item);
				}
				this.stackNews.UpdateLayout();
				this.newsHeight = this.stackNews.ActualHeight;
				if (this.newsHeight > this.news_list.ActualHeight)
				{
					foreach (string str in NewsStrings)
					{
						item = new NewsItem();
						item.Margin = new Thickness(0.0, 2.0, 0.0, 0.0);
						item.SetNewsString(str);
						this.stackNews.Children.Add(item);
					}
					this.showNewsListTimer.Start();
				}
				else
				{
					this.showNewsListTimer.Stop();
				}
			}
		}

		private void sb_Completed(object sender, EventArgs e)
		{
			this.showNewsListTimer.Start();
		}

		private void showNewsListTimer_Tick(object sender, EventArgs e)
		{
			if (this.stackNews.Children.Count > 0)
			{
				if (-this.newsStackTop >= this.newsHeight)
				{
					this.newsStackTop = 0.0;
					this.posItem = 0;
				}
				if (this.posItem >= (this.stackNews.Children.Count - 1))
				{
					this.posItem = 0;
				}
				this.sb.Stop();
				this.sb.Children.Clear();
				NewsItem item = this.stackNews.Children[posItem++] as NewsItem;
				DoubleAnimation animation = new DoubleAnimation();
				animation.From = new double?(this.newsStackTop);
				this.newsStackTop -= item.RenderSize.Height + 2.0;
				animation.To = new double?(this.newsStackTop);
				animation.Duration = new Duration(TimeSpan.FromSeconds(item.RenderSize.Height / 100.0));
				Storyboard.SetTarget(animation, this.newsTransform);
				Storyboard.SetTargetProperty(animation, new PropertyPath("Y", new object[0]));
				this.sb.Children.Add(animation);
				this.sb.Begin();
				this.showNewsListTimer.Stop();
			}
		}
	}
}
